package Java7_Concurrency_Cookbook.thread_executor_framework.rejected_execution_handler;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 为了处理被拒绝的任务,需要创建一个实现 RejectedExecutionHandler 接口的处理类<br/>
 * 被执行器拒绝的每一个任务都将调用这个方法.
 * @author lixiangke
 * @date 2017/11/21
 */
public class Main {
    public static void main(String[] args) {
        RejectedTaskController controller = new RejectedTaskController();
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
        executor.setRejectedExecutionHandler(controller);
        System.out.printf("Main: Starting\n");
        for (int i = 0; i < 3; i++) {
            Task task = new Task("Zoom" + i);
            executor.submit(task);
        }
        System.out.printf("Main: Shutting down the executor\n");
        executor.shutdown();

        System.out.printf("Main: Sending another Zoom.\n");
        Task task = new Task("RejectedTask");
        executor.submit(task);

        System.out.printf("Main: End\n");
    }
}
